//
//  LZTabBarController.m
//  Project
//
//  Created by liting on 2025/8/29.
//

#import "LZTabBarController.h"

@interface LZTabBarController ()

@end

@implementation LZTabBarController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupDefaultAppearance];
}

#pragma mark - 初始化方法
+ (instancetype)tabBarControllerWithViewControllers:(NSArray<UIViewController *> *)viewControllers
                                             titles:(NSArray<NSString *> *)titles
                                             images:(NSArray<NSString *> *)images
                                     selectedImages:(NSArray<NSString *> *)selectedImages {

    LZTabBarController *tabBarController = [[LZTabBarController alloc] init];

    NSMutableArray *navControllers = [NSMutableArray array];
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *vc = viewControllers[i];

        // 设置标签项
        if (i < titles.count) {
            vc.tabBarItem.title = titles[i];
        }
        if (i < images.count) {
            vc.tabBarItem.image = [[UIImage imageNamed:images[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        if (i < selectedImages.count) {
            vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImages[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }

        // 添加到导航控制器
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [navControllers addObject:nav];
    }

    tabBarController.viewControllers = navControllers;
    return tabBarController;
}

#pragma mark - 公共方法
- (void)addViewController:(UIViewController *)viewController
                    title:(NSString *)title
                    image:(NSString *)image
            selectedImage:(NSString *)selectedImage {

    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];

    NSMutableArray *currentViewControllers = [self.viewControllers mutableCopy];
    if (!currentViewControllers) {
        currentViewControllers = [NSMutableArray array];
    }
    [currentViewControllers addObject:nav];
    self.viewControllers = currentViewControllers;
}

- (void)setTabBarTintColor:(UIColor *)tintColor
              barTintColor:(UIColor *)barTintColor
           unselectedColor:(UIColor *)unselectedColor {

    // 设置选中颜色为黑色
    self.tabBar.tintColor = [UIColor blackColor];

    if (barTintColor) {
        self.tabBar.barTintColor = barTintColor;
    }

    // 设置未选中颜色
    if (unselectedColor) {
        if (@available(iOS 10.0, *)) {
            self.tabBar.unselectedItemTintColor = unselectedColor;
        }

        // iOS 10 以下兼容
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: unselectedColor}
                                                 forState:UIControlStateNormal];
    }

    // 设置选中状态的文字属性
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}
                                             forState:UIControlStateSelected];

    // 配置 iOS 13+ 的外观
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];

        // 选中状态
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{
            NSForegroundColorAttributeName: [UIColor blackColor]
        };
        appearance.stackedLayoutAppearance.selected.iconColor = [UIColor blackColor];

        // 未选中状态
        if (unselectedColor) {
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{
                NSForegroundColorAttributeName: unselectedColor
            };
            appearance.stackedLayoutAppearance.normal.iconColor = unselectedColor;
        }

        self.tabBar.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = appearance;
        }
    }
}

- (void)showBadgeValue:(NSString *)badgeValue atIndex:(NSInteger)index {
    if (index < self.viewControllers.count) {
        UIViewController *vc = self.viewControllers[index];
        vc.tabBarItem.badgeValue = badgeValue;

        // 小红点样式
        if ([badgeValue isEqualToString:@""] || badgeValue == nil) {
            vc.tabBarItem.badgeValue = @"";
            vc.tabBarItem.badgeColor = [UIColor redColor];
        }
    }
}

- (void)hideBadgeAtIndex:(NSInteger)index {
    if (index < self.viewControllers.count) {
        UIViewController *vc = self.viewControllers[index];
        vc.tabBarItem.badgeValue = nil;
    }
}

- (void)setCenterButton:(UIButton *)button atIndex:(NSInteger)index {
    // 需要配合自定义 TabBar 实现，这里提供接口
}

#pragma mark - 私有方法
- (void)setupDefaultAppearance {
    // 默认外观设置
    self.tabBar.translucent = NO;
    self.tabBar.backgroundColor = [UIColor whiteColor];

    // 去除顶部线条
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.shadowColor = [UIColor clearColor];
        self.tabBar.standardAppearance = appearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = appearance;
        }
    } else {
        self.tabBar.shadowImage = [[UIImage alloc] init];
        self.tabBar.backgroundImage = [[UIImage alloc] init];
    }
}

@end
